I'm a code enthusiastic final year student pursuing B.Tech in ECE currently in final year and final semester.However, I've done relevant internships as well in the domain of software development and content writing.
PEP, or Python Enhancement Proposal, refers to several documents that describe new features that are proposed for Python and document aspects of Python, such as design and style. These documents are released to the developer community.
PEP 8 (also called PEP8 or PEP-8) is a PEP document that provides guidelines and the best practices for writing Python code. Written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan, the main focus of PEP 8 is to improve the readability and consistency of Python code.
A few of the key guidelines to be followed according to PEP 8 are:
Naming Conventions
Choosing sensible names that will save you time and energy later. For example, avoiding single-letter names like l and O is more efficient, as these can look similar to 1 and 0 which may confuse.
2. Naming Styles
Functions: Use the lowercase word(s) separated by underscores. Example: my_function()
b. Variables: Use lowercase single letters or word(s) separated by underscores. Examples: x, my_variable
Class: Use camel case for all class names with no space between words. Example: MyClass
Constants: Use uppercase single letter or word(s) separated by underscores. Examples: CONSTANT, MY_CONSTANT
Packages: Use the short lowercase word(s). Do not separate the words with underscores. Example: mypackage
3. Code Layout
a. Using blank lines:
Surround top-level functions and classes with two blank lines.
class MyClass1:
pass
class MyClass2:
pass
def top_level_function():
return 1
Surround method definitions inside classes with a single blank line.
class MyClass:
def method_one(self):
return 1
def method_two(self):
return 2
Use blank lines sparingly within the functions to show clear steps. This can be done at the programmer's discretion
b. Maximum Line Length:
PEP 8 suggests lines be limited to 79 characters in length to allow users to have multiple files open next to one another while avoiding line wrapping.
c. Indentation:
The key indentation rules laid out in PEP 8 are the following:
Use 4 consecutive spaces to indicate indentation.
Prefer spaces over tabs.
d. Lining up the Closing Brace:
PEP 8 provides two options for the position of the closing brace:
Line up the closing brace with the first non-whitespace character of the previous line.
list = [
1, 2, 3,
4, 5, 6,
7, 8, 9
]
Line up the closing brace with the first character of the line that starts the construct.
list = [
1, 2, 3,
4, 5, 6,
7, 8, 9
]
e. Comments:
One should limit the line length of comments and docstrings to 72 characters. Make sure to use complete sentences, starting with a capital letter. Do not forget to update comments if you change your code.
Liked By
Write Answer
What is meant by PEP 8 style guide?Give suitable example.
Join MindStick Community
You have need login or register for voting of answers or question.
Krishnapriya Rajeev
03-Apr-2023PEP, or Python Enhancement Proposal, refers to several documents that describe new features that are proposed for Python and document aspects of Python, such as design and style. These documents are released to the developer community.
PEP 8 (also called PEP8 or PEP-8) is a PEP document that provides guidelines and the best practices for writing Python code. Written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan, the main focus of PEP 8 is to improve the readability and consistency of Python code.
A few of the key guidelines to be followed according to PEP 8 are:
Choosing sensible names that will save you time and energy later. For example, avoiding single-letter names like l and O is more efficient, as these can look similar to 1 and 0 which may confuse.
2. Naming Styles
3. Code Layout
a. Using blank lines:
Surround top-level functions and classes with two blank lines.
Use blank lines sparingly within the functions to show clear steps. This can be done at the programmer's discretion
b. Maximum Line Length:
PEP 8 suggests lines be limited to 79 characters in length to allow users to have multiple files open next to one another while avoiding line wrapping.
c. Indentation:
The key indentation rules laid out in PEP 8 are the following:
Use 4 consecutive spaces to indicate indentation.
Prefer spaces over tabs.
d. Lining up the Closing Brace:
Line up the closing brace with the first non-whitespace character of the previous line.
e. Comments:
One should limit the line length of comments and docstrings to 72 characters. Make sure to use complete sentences, starting with a capital letter. Do not forget to update comments if you change your code.